package com.guimin.order.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guimin.order.entity.GuiminOrder;
import com.guimin.order.service.GuiminOrderService;
import com.guimin.user.api.UserInfoApi;
import com.guimin.user.dto.LoginInfoDto;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 社区订单信息表(GuiminOrder)控制层
 *
 * @author makejava
 * @since 2021-11-23 17:40:02
 */
@RestController
@RequestMapping("guiminOrder")
public class GuiminOrderApi {
    /**
     * 服务对象
     */
    @Resource
    private GuiminOrderService guiminOrderService;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redis;
    @Resource
    private UserInfoApi userInfoApi;

    @GetMapping("/list")
    public R<IPage<GuiminOrder>> list(HttpServletRequest request,
                                      @RequestParam(defaultValue = "1") Integer current,
                                      @RequestParam(defaultValue = "10") Integer size) {
        LoginInfoDto login = userInfoApi.getLogin(request);
        IPage<GuiminOrder> page = guiminOrderService.page(new Page<>(current, size), new LambdaQueryWrapper<GuiminOrder>()
                .eq(GuiminOrder::getPayerId, login.getId())
                .orderByDesc(GuiminOrder::getId));
        return R.ok(page);
    }

    @GetMapping("/all")
    public R<IPage<GuiminOrder>> allList(@RequestParam(defaultValue = "1") Integer current,
                                         @RequestParam(defaultValue = "10") Integer size) {
        IPage<GuiminOrder> page = guiminOrderService.page(new Page<>(current, size), new LambdaQueryWrapper<GuiminOrder>()
                .select(GuiminOrder::getId, GuiminOrder::getType, GuiminOrder::getAmount, GuiminOrder::getMemo, GuiminOrder::getCreateDatetime,
                        GuiminOrder::getState)
        );
        return R.ok(page);
    }

}

